Ext.ns("crs.grid");

crs.grid.UserReviewGridPanel = Ext.extend(Ext.grid.EditorGridPanel, {

	/* begin of fields initialization */
	autoHeight : true,
	review_url : 'review_reviewApplicant.do',
	default_url : 'review_applicantsPendingReview.do',
	defaultProxy : '',
	pageSize : 15,
	closable : true,
	params : '',
	detailWindow : {},
	detailForm : {},
	root : 'listUser',
	totalProperty : 'pagination.totalCount',
	title : '待审核用户',
	viewConfig : {
		deferEmptyText : false,
		emptyText : '没有可显示数据',
		forceFit : true
	},
	params : {},
	defaultProxy : {},
	fields : ['id', 'userName', 'companyName', 'IDType', 'IDNumber',
			'detailAddress', 'postalcode', 'contactNumber', 'phoneNumber',
			'fax', 'email', 'contact', 'registerTime'],
	columns : [{
				header : 'id',
				hidden : true,
				dataIndex : 'id'
			}, {
				header : '用户名',
				dataIndex : 'userName'
			}, {
				header : '单位名称或姓名',
				dataIndex : 'companyName'
			}/*, {
				header : '证件类型',
				dataIndex : 'IDType',
				renderer : function(value) {
					var result = value;
					idTypeStore.each(function(record) {
								if (record.data.value == value) {
									result = record.data.text;
								}
							});

					return result;
				}
			}, {
				header : '证件号码',
				dataIndex : 'IDNumber'
			}*/, {
				header : '联系人',
				dataIndex : 'contact'
			},{
				header : '电话号码',
				dataIndex : 'contactNumber'
			},{
				header : '手机号码',
				dataIndex : 'phoneNumber'
			},{
				header : 'E-mail',
				dataIndex : 'email'
			}, {
				header : '注册时间',
				dataIndex : 'registerTime'
			}],
	sm : new Ext.grid.RowSelectionModel({
				singleSelect : true
			}),
	initComponent : function() {
		this.buildProxies();
		this.params = this.buildParams();
		this.store = this.buildStore();
		// this.columns = this.buildColumns();
		this.tbar = this.buildTbar();
		this.bbar = this.buildBbar();

		this.initDetailForm();
		crs.grid.UserReviewGridPanel.superclass.initComponent.call(this);

		this.store.load(this.params);
	},

	initDetailForm : function() {
		this.detailForm = new Ext.form.FormPanel({
					frame : true,
					title : '详细信息',
					labelAlign : 'right',
					labelStyle : 'font-weight:bold;',
					labelWidth : 100,
					width : 700,
					items : [{
								layout : 'column',
								items : [{ // column #1
									columnWidth : .5,
									layout : 'form',

									items : [{
												xtype : 'textfield',
												fieldLabel : '用户名',
												name : 'userName',
												disabled : true,
												anchor : '80%'

											}, {
												xtype : 'combo',
												fieldLabel : '证件类型',
												valueField : 'value',
												displayField : 'text',
												name : 'IDType',
												hiddenName : 'IDType',
												disabled : true,
												anchor : '80%',
												mode : 'local',
												store : idTypeStore
											}, {
												xtype : 'textfield',
												fieldLabel : '联系人',
												disabled : true,
												name : 'contact',
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : '手机号码',
												name : 'phoneNumber',
												disabled : true,
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : 'E-mail',
												disabled : true,
												name : 'email',
												anchor : '80%'

											}, {
												xtype : 'textarea',
												fieldLabel : '详细地址',
												disabled : true,
												name : 'detailAddress',
												anchor : '80%'

											}]
										// close items for first column
								}, {
									columnWidth : .5,
									layout : 'form',
									items : [{
												xtype : 'textfield',
												fieldLabel : '单位名称或姓名',
												name : 'companyName',
												disabled : true,
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : '证件号码',
												disabled : true,
												name : 'IDNumber',
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : '邮政编码',
												disabled : true,
												name : 'postalcode',
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : '电话号码',
												name : 'contactNumber',
												disabled : true,
												anchor : '80%'

											}, {
												xtype : 'textfield',
												fieldLabel : '注册时间',
												name : 'registerTime',
												disabled : true,
												anchor : '80%'

											}, {
												xtype : 'hidden',
												name : 'id'
											}]
										// close items for second column
									}]
							}]
				});

		this.detailWindow = new Ext.Window({
					frame : true,
					plain : true,
					modal : true,
					resizable : false,
					title : '详细信息',
					buttonAlign : "right",
					closeAction : "hide",
					closable : true,
					bodyStyle : "padding:4px",
					width : 700,
					autoHeight : true,
					layout : "form",
					items : [this.detailForm],
					buttons : [{
								text : "审核通过",
								handler : this.doReview,
								scope : this
							},{
								text : "审核不通过",
								handler : this.doRefuse,
								scope : this
							}, {
								text : "取消",
								handler : function() {
									this.detailWindow.hide();
								},
								scope : this
							}]
				});
	},
	buildTbar : function() {
		return new Ext.Toolbar({
					items : ['-', {
								text : '详细信息',
								handler : this.showDetail,
								scope : this
							}, '-']
				});
	},

	/* end of components appending to this panel */
	buildStore : function() {

		return new Ext.data.Store({
					proxy : this.defaultProxy,
					reader : new Ext.data.JsonReader({
								root : this.root,
								totalProperty : this.totalProperty,
								fields : this.fields
							})
				});

	},

	buildProxies : function() {

		this.defaultProxy = new Ext.data.HttpProxy({
					url : this.default_url,
					method : 'post'
				});
	},
	buildBbar : function() {
		return new Ext.PagingToolbar({
					store : this.store,
					pageSize : this.pageSize,
					displayInfo : true
				});
	},
	buildParams : function() {
		var dataPost = {};
		dataPost.params = {};
		dataPost.params.start = 0;
		dataPost.params.limit = this.pageSize;
		return dataPost;
	},

	/* begin of handler implementation */

	showDetail : function() {
		this.detailForm.getForm().reset();
		if (this.selModel.getSelections().length < 1) {
			Ext.Msg.alert('提示', '请选择一项!');
			return;
		}

		this.detailWindow.show();

		var selRec = this.selModel.getSelections()[0];
		this.detailForm.getForm().loadRecord(selRec);

	},
	doRefuse : function() {
		if (this.selModel.getSelections().length < 1) {
			Ext.Msg.alert('提示', '请选择一项!');
			return;
		}

		this.el.mask("审核中...", 'x-mask-loading');

		Ext.Ajax.request({
					url : this.review_url,
					params : {
						// mapping to field named 'msisdn' of
						// mobileUserSearchAction at the backend
						'user.id' : this.selModel.getSelections()[0].id,
						'user.status' : 4
					},
					success : function(resp) {
						var rp = Ext.decode(resp.responseText);
						this.el.unmask();
						this.detailWindow.hide();
						if (rp.success == true || rp.success == 'true') {
							Ext.MessageBox.alert('提示', '审核不通过!');
							this.store.load(this.params);
						} else{
							Ext.MessageBox.alert('错误', '错误信息:' + rp.message);
						}
						
					},
					scope : this

				});
	},

	doReview : function() {

		if (this.selModel.getSelections().length < 1) {
			Ext.Msg.alert('提示', '请选择一项!');
			return;
		}

		this.el.mask("审核中...", 'x-mask-loading');

		Ext.Ajax.request({
					url : this.review_url,
					params : {
						// mapping to field named 'msisdn' of
						// mobileUserSearchAction at the backend
						'user.id' : this.selModel.getSelections()[0].id,
						'user.status' : 2
					},
					success : function(resp) {
						var rp = Ext.decode(resp.responseText);
						this.el.unmask();
						this.detailWindow.hide();
						if (rp.success == true|| rp.success == 'true') {
							this.store.load(this.params);
						} else {
							Ext.MessageBox.alert('错误', '错误信息:' + rp.message);
						}
						
					},
					scope : this

				});
	}
		/* end of handler implementation */
	});

Ext.reg("user_review_grid", crs.grid.UserReviewGridPanel);